import { getBatchProductByPage, addBatchProduct, updateBatchProduct, delBatchProduct, exportBatchProduct } from '@/services/project/batchProduct';
import { PlusOutlined } from '@ant-design/icons';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { Button, Form, Input, message, Modal, Tree } from 'antd';
import { sortTran, filterTran } from '@/utils/requestUtil';
import React, { useEffect, useRef, useState } from 'react';
const BatchProduct: React.FC<{}> = () => {
const actionRef = useRef<ActionType>();
  const [modalVisit, setModalVisit] = useState(false); // 新建和编辑提示框
  const [confirmLoading, setConfirmLoading] = useState(false); //编辑框的disable
  const [formTitle, setFormTitle] = useState('新建'); //
  const [createOrUpdate, setCreateOrUpdate] = useState(false); //编辑框的disable
  const [form] = Form.useForm();
    const ref = useRef(null);
  const columns: ProColumns[] = [
        {
        title: '序号',
        dataIndex: 'index',
        valueType: 'index',
        width: 80,
        },
        {
          title: '实际设计工时',
          dataIndex: 'actualDesignWh',
        },
        {
          title: '实际生产工时',
          dataIndex: 'actualManufactWh',
        },
        {
          title: '交货日期',
          dataIndex: 'batchDeliveryDate',
        },
        {
          title: '计划编码',
          dataIndex: 'batchPlanCode',
        },
        {
          title: '批次计划id',
          dataIndex: 'batchPlanId',
        },
        {
          title: 'BOMID',
          dataIndex: 'bomId',
        },
        {
          title: 'BOM上传时间',
          dataIndex: 'bomTime',
        },
        {
          title: '设计等级',
          dataIndex: 'desighWhLevel',
        },
        {
          title: '图纸ID',
          dataIndex: 'drawingId',
        },
        {
          title: '图纸上传时间',
          dataIndex: 'drawingTime',
        },
        {
          title: '附件ID',
          dataIndex: 'fileId',
        },
        {
          title: '上传BOM',
          dataIndex: 'isBomUploaded',
        },
        {
          title: '上传图纸',
          dataIndex: 'isDrawingUploaded',
        },
        {
          title: 'PO释放',
          dataIndex: 'isPoReleased',
        },
        {
          title: '标准柜体',
          dataIndex: 'isStandard',
        },
        {
          title: 'WO释放',
          dataIndex: 'isWoReleased',
        },
        {
          title: '生产等级',
          dataIndex: 'manufactWhLevel',
        },
        {
          title: '计划设计工时',
          dataIndex: 'planDesignWh',
        },
        {
          title: '计划生产工时',
          dataIndex: 'planManufactWh',
        },
        {
          title: 'POID',
          dataIndex: 'poId',
        },
        {
          title: 'PO释放时间',
          dataIndex: 'poTime',
        },
        {
          title: '柜体编码',
          dataIndex: 'productCode',
        },
        {
          title: '柜体数量',
          dataIndex: 'productQty',
        },
        {
          title: '柜体类型',
          dataIndex: 'productType',
        },
        {
          title: '审核人员',
          dataIndex: 'reviewEditor',
        },
        {
          title: '审核附件ID',
          dataIndex: 'reviewFileId',
        },
        {
          title: '审核备注',
          dataIndex: 'reviewMemo',
        },
        {
          title: '审核状态:0-初始化 待上传 1-待审核 2-审核通过 3-冻结  如有异常直接退回0状态',
          dataIndex: 'reviewState',
        },
        {
          title: '图纸上传时间',
          dataIndex: 'reviewTime',
        },
        {
          title: '流水号',
          dataIndex: 'serialNo',
        },
        {
          title: 'WOID',
          dataIndex: 'woId',
        },
        {
          title: 'WO释放时间',
          dataIndex: 'woTime',
        },
        {
          title: '待分配柜体数量',
          dataIndex: 'unAllocatedQty',
        },
        {
          title: '项目id',
          dataIndex: 'infoId',
        },
        {
          title: '项目名称',
          dataIndex: 'projectName',
        },
        {
          title: '非标柜体名称',
          dataIndex: 'unstandardName',
        },
        {
        title: '创建日期', //表头显示的名称
        search: false,
        dataIndex: 'createTime',
        },
        {
        title: '创建人', //表头显示的名称
        search: false,
        dataIndex: 'createName',
        },
        {
        title: '操作',
        valueType: 'option',
        key: 'option',
        fixed: 'right',
        render: (text, record, _, action) => [
        <a
                key="editKey"
                onClick={() => {

        setModalVisit(true);
        setCreateOrUpdate(true);
        setFormTitle('编辑');
        form.setFieldsValue({
        actualDesignWh: record.actualDesignWh,
        actualManufactWh: record.actualManufactWh,
        batchDeliveryDate: record.batchDeliveryDate,
        batchPlanCode: record.batchPlanCode,
        batchPlanId: record.batchPlanId,
        bomId: record.bomId,
        bomTime: record.bomTime,
        desighWhLevel: record.desighWhLevel,
        drawingId: record.drawingId,
        drawingTime: record.drawingTime,
        fileId: record.fileId,
        isBomUploaded: record.isBomUploaded,
        isDrawingUploaded: record.isDrawingUploaded,
        isPoReleased: record.isPoReleased,
        isStandard: record.isStandard,
        isWoReleased: record.isWoReleased,
        manufactWhLevel: record.manufactWhLevel,
        planDesignWh: record.planDesignWh,
        planManufactWh: record.planManufactWh,
        poId: record.poId,
        poTime: record.poTime,
        productCode: record.productCode,
        productQty: record.productQty,
        productType: record.productType,
        reviewEditor: record.reviewEditor,
        reviewFileId: record.reviewFileId,
        reviewMemo: record.reviewMemo,
        reviewState: record.reviewState,
        reviewTime: record.reviewTime,
        serialNo: record.serialNo,
        woId: record.woId,
        woTime: record.woTime,
        unAllocatedQty: record.unAllocatedQty,
        infoId: record.infoId,
        projectName: record.projectName,
        unstandardName: record.unstandardName,
        id: record.id,
        });
        }}
        >
        编辑
        </a>,
        <a
                key="deleteKey"
                onClick={() => {


        Modal.confirm({
        title: '删除',
        content: '确定要删除吗',
        okText: '确认',
        cancelText: '取消',
        onOk: async () => {
        await delBatchProduct({ id: record.id });
        actionRef.current.reload();
        },
        });

        }}
        >
        删除
        </a>,
        ],
        },
  ];
  const handleSubmit = async () => {
        setConfirmLoading(true);
        if (formTitle == '新建') {
          const result = await addBatchProduct(form.getFieldsValue());
        if (result != null) {
          message.success('添加成功');
        }
        } else {
          await updateBatchProduct(form.getFieldsValue());
        }
        setConfirmLoading(false);
        setModalVisit(false);
        actionRef.current.reload();
  };

  return (
  <PageHeaderWrapper>
    <ProTable
        headerTitle="xx查询"
        actionRef={actionRef} //用于触发刷新操作等，看api
        rowKey="id" //表格行 key 的取值，可以是字符串或一个函数
        formRef={ref}
        toolBarRender={(action, { selectedRows }) => [
              <Button
                key="button"
                icon={<PlusOutlined />}
                onClick={() => {
                    setCreateOrUpdate(false);
                    setFormTitle('新建');
                    form.resetFields();
                    setModalVisit(true);
                }}
                type="primary"
                >
                新建
                </Button>,
                <Button
                        key="button"
                        icon={<PlusOutlined />}
                onClick={() => {
                const filters = filterTran(ref.current.getFieldsValue());
                exportBatchProduct({
                    current: 1,
                    pageSize: 5000,
                    filters: filters,
                }).then((res) => {
                const aLink = document.createElement('a');
                document.body.appendChild(aLink);
                aLink.style.display = 'none';
                aLink.href = window.URL.createObjectURL(res);
                aLink.setAttribute('download', `BatchProduct.xlsx`);
                aLink.click();
                document.body.removeChild(aLink);
                });
                }}
                type="primary"
                >
                导出
                </Button>,
        ]}
        request={async (params = {}, sort, filter) => {

                const sorters = sortTran(sort);
                const filters = filterTran(params);
                const result = await getBatchProductByPage({ ...params, sorters: sorters, filters: filters });
                return {
                data: result.data.content,
                total: result.data.totalElements,
                success: result.success,
                };

        }}
        columns={columns}
        scroll={{ x: 2000, y: 350 }}
        rowSelection={{}}
    />
    <Modal
            title={formTitle}
            visible={modalVisit}
            onOk={handleSubmit}
            confirmLoading={confirmLoading}
            onCancel={() => setModalVisit(false)}
    >
    <Form
            name="wrap"
            labelCol={{ flex: '110px' }}
            labelAlign="left"
            labelWrap
            form={form}
            wrapperCol={{ flex: 1 }}
            colon={false}
    >
          <Form.Item label="id" name="id" hidden={true}>
            <Input />
          </Form.Item>
          <Form.Item
                  label="实际设计工时"
                  name="actualDesignWh"
                  rules={[{ required: true, message: 'Please input your actualDesignWh!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="实际生产工时"
                  name="actualManufactWh"
                  rules={[{ required: true, message: 'Please input your actualManufactWh!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="交货日期"
                  name="batchDeliveryDate"
                  rules={[{ required: true, message: 'Please input your batchDeliveryDate!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="计划编码"
                  name="batchPlanCode"
                  rules={[{ required: true, message: 'Please input your batchPlanCode!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="批次计划id"
                  name="batchPlanId"
                  rules={[{ required: true, message: 'Please input your batchPlanId!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="BOMID"
                  name="bomId"
                  rules={[{ required: true, message: 'Please input your bomId!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="BOM上传时间"
                  name="bomTime"
                  rules={[{ required: true, message: 'Please input your bomTime!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="设计等级"
                  name="desighWhLevel"
                  rules={[{ required: true, message: 'Please input your desighWhLevel!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="图纸ID"
                  name="drawingId"
                  rules={[{ required: true, message: 'Please input your drawingId!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="图纸上传时间"
                  name="drawingTime"
                  rules={[{ required: true, message: 'Please input your drawingTime!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="附件ID"
                  name="fileId"
                  rules={[{ required: true, message: 'Please input your fileId!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="上传BOM"
                  name="isBomUploaded"
                  rules={[{ required: true, message: 'Please input your isBomUploaded!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="上传图纸"
                  name="isDrawingUploaded"
                  rules={[{ required: true, message: 'Please input your isDrawingUploaded!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="PO释放"
                  name="isPoReleased"
                  rules={[{ required: true, message: 'Please input your isPoReleased!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="标准柜体"
                  name="isStandard"
                  rules={[{ required: true, message: 'Please input your isStandard!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="WO释放"
                  name="isWoReleased"
                  rules={[{ required: true, message: 'Please input your isWoReleased!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="生产等级"
                  name="manufactWhLevel"
                  rules={[{ required: true, message: 'Please input your manufactWhLevel!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="计划设计工时"
                  name="planDesignWh"
                  rules={[{ required: true, message: 'Please input your planDesignWh!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="计划生产工时"
                  name="planManufactWh"
                  rules={[{ required: true, message: 'Please input your planManufactWh!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="POID"
                  name="poId"
                  rules={[{ required: true, message: 'Please input your poId!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="PO释放时间"
                  name="poTime"
                  rules={[{ required: true, message: 'Please input your poTime!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="柜体编码"
                  name="productCode"
                  rules={[{ required: true, message: 'Please input your productCode!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="柜体数量"
                  name="productQty"
                  rules={[{ required: true, message: 'Please input your productQty!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="柜体类型"
                  name="productType"
                  rules={[{ required: true, message: 'Please input your productType!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="审核人员"
                  name="reviewEditor"
                  rules={[{ required: true, message: 'Please input your reviewEditor!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="审核附件ID"
                  name="reviewFileId"
                  rules={[{ required: true, message: 'Please input your reviewFileId!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="审核备注"
                  name="reviewMemo"
                  rules={[{ required: true, message: 'Please input your reviewMemo!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="审核状态:0-初始化 待上传 1-待审核 2-审核通过 3-冻结  如有异常直接退回0状态"
                  name="reviewState"
                  rules={[{ required: true, message: 'Please input your reviewState!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="图纸上传时间"
                  name="reviewTime"
                  rules={[{ required: true, message: 'Please input your reviewTime!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="流水号"
                  name="serialNo"
                  rules={[{ required: true, message: 'Please input your serialNo!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="WOID"
                  name="woId"
                  rules={[{ required: true, message: 'Please input your woId!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="WO释放时间"
                  name="woTime"
                  rules={[{ required: true, message: 'Please input your woTime!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="待分配柜体数量"
                  name="unAllocatedQty"
                  rules={[{ required: true, message: 'Please input your unAllocatedQty!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="项目id"
                  name="infoId"
                  rules={[{ required: true, message: 'Please input your infoId!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="项目名称"
                  name="projectName"
                  rules={[{ required: true, message: 'Please input your projectName!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="非标柜体名称"
                  name="unstandardName"
                  rules={[{ required: true, message: 'Please input your unstandardName!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
        </Form>
      </Modal>
  </PageHeaderWrapper>
  );
  };
  export default BatchProduct;
